package com.cnfangmao.masterdata.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnfangmao.masterdata.dao.BasicDataMetadataDao;
import com.cnfangmao.masterdata.dto.BasicDataMetadataSearchDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.BasicDataMetadata;
import com.cnfangmao.masterdata.service.BasicDataMetadataService;

/**
 * <p>
 * 主数据元数据 服务实现类
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@Service
public class BasicDataMetadataServiceImpl extends ServiceImpl<BasicDataMetadataDao, BasicDataMetadata> implements BasicDataMetadataService {
	
	@Autowired
	private BasicDataMetadataDao dao;
	
	@Override
	public BasicDataMetadata findByMetadataCode(String metadataCode) {
		BasicDataMetadata basicDataMetadata = null;
		QueryWrapper<BasicDataMetadata> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("metadata_code", metadataCode);
        basicDataMetadata = dao.selectOne(queryWrapper);
		return basicDataMetadata;
	}

	@Override
	public BasicDataMetadata findByMetadataName(String metadataName) {
		BasicDataMetadata basicDataMetadata = null;
		QueryWrapper<BasicDataMetadata> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("metadata_name", metadataName);
        basicDataMetadata = dao.selectOne(queryWrapper);
		return basicDataMetadata;
	}
	
	@Override
	public IPage<BasicDataMetadata> queryListBasicDataMetadata(
			GeneralSearchDto<BasicDataMetadataSearchDto> dto) {

		IPage<BasicDataMetadata> page = dao.selectPage(new Page<>(dto.getPageNo(), dto.getPageSize()),
				new QueryWrapper<BasicDataMetadata>().
				like(StringUtils.isNotEmpty(dto.getConditions().getMetadataName()), "metadata_name", dto.getConditions().getMetadataName())
				.or().like(StringUtils.isNotEmpty(dto.getConditions().getMetadataCode()), "metadata_code", dto.getConditions().getMetadataCode())
				);

		return page; 
		
	}

}
